/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OprationsUI.java
 *
 * Created on Mar 17, 2011, 12:50:11 PM
 */

package Taxi;

import Taxi.db.Database;
import Taxi.model.Driver;
import Taxi.db.dao.DriverDAO;
import Taxi.db.dao.OperationsDAO;
import Taxi.model.Taxi;
import Taxi.db.dao.TaxiDAO;
import Taxi.model.Operation;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author BIGBOSS
 */
public class OperationsUI extends javax.swing.JFrame {

    private OperationsDAO operationsDAO;
    private TaxiDAO taxiDAO;
    private DriverDAO employeeDAO;
    
    /** Creates new form OprationsUI */
    public OperationsUI() throws SQLException, ClassNotFoundException {
        operationsDAO = new OperationsDAO();
        taxiDAO = new TaxiDAO();
        employeeDAO = new DriverDAO();
        initComponents();
        operationsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                // disable/enable EditButton
                int selRow = operationsTable.getSelectedRow();
                editButton.setEnabled(selRow != -1);
                deleteButton.setEnabled(selRow != -1);
            }
        });
        loadData();
    }

    public Operation getOperationFromTable() throws NumberFormatException {
//        //"Taxi Number", "Driver", "Start Odometer", "End Odometer", "Plate Number", "Date", "Boundary"
//        int selRow = operationsTable.getSelectedRow();
//        int id = Integer.parseInt(operationsTable.getModel().getValueAt(selRow, 0).toString());
//        String unitnumber = operationsTable.getModel().getValueAt(selRow, 1).toString();
//        String driver = operationsTable.getModel().getValueAt(selRow, 2).toString();
//        String startodometer = operationsTable.getModel().getValueAt(selRow, 3).toString();
//        String endodometer = operationsTable.getModel().getValueAt(selRow, 4).toString();
//        String platenumber = operationsTable.getModel().getValueAt(selRow, 5).toString();
//        String date = operationsTable.getModel().getValueAt(selRow, 6).toString();
//        String boundary = operationsTable.getModel().getValueAt(selRow, 7).toString();
//        Operation operation = new Operation(unitnumber, driver, startodometer, endodometer, platenumber, date, boundary);
//        operation.setId(id);
//        return operation;
        return null;
    }

    public void updateOperationTable() {
        operationsTable.setModel(new OperationsTableModel(getResultFromDB()));
    }

    private class TaxiCMBModel extends DefaultComboBoxModel{

        public TaxiCMBModel(Object[] array) {
            super(array);
        }
        

        @Override
        public void setSelectedItem(Object anObject) {
            super.setSelectedItem(anObject);
            Taxi taxi = null;
            try {
                taxi = taxiDAO.getTaxiByPlateNumber(anObject.toString());
            } catch (SQLException ex) {
                Logger.getLogger(OperationsUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("taxi.getPlateNumber():" + taxi.getPlateNumber());
            plateField.setText(taxi.getPlateNumber());
        }

    }

    private class EmployeeCMBModel extends DefaultComboBoxModel{
        List<Driver> employees;
        public EmployeeCMBModel(List<Driver> employees) {
            super(employees.toArray());
            this.employees = employees;
        }

        @Override
        public Object getElementAt(int index) {
            Driver employee = (Driver) super.getElementAt(index);
            return employee.getFirstName() + " " + employee.getLastName();
        }
    }

    private void loadData() throws SQLException{
        List<Integer> taxiUnitNums = taxiDAO.getAllTaxiUnitNumber();
        TaxiCMBModel taxiCMBModel = new TaxiCMBModel(taxiUnitNums.toArray());
        EmployeeCMBModel employeeCMBModel = new EmployeeCMBModel(employeeDAO.getAllEmployee());
        taxiNumberCombo.setModel(taxiCMBModel);
        driverCombo.setModel(employeeCMBModel);
        if(!taxiUnitNums.isEmpty()){
            Taxi taxi = taxiDAO.getTaxiByPlateNumber(taxiUnitNums.get(0).toString());
            plateField.setText(taxi.getPlateNumber());
        }
    }

    public ResultSet getResultFromDB() {
        ResultSet rs=null;
        Statement stmt = null;
        try{
            stmt = Database.getConnection().createStatement();
            rs=(ResultSet) stmt.executeQuery("Select * from Operation");
        }
        catch(SQLException e){
            e.printStackTrace();
        }
        return rs;
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dataPanel = new javax.swing.JPanel();
        numberLabel = new javax.swing.JLabel();
        taxiNumberCombo = new javax.swing.JComboBox();
        driverLabel = new javax.swing.JLabel();
        driverCombo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        startLabel = new javax.swing.JLabel();
        startField = new javax.swing.JTextField();
        endLabel = new javax.swing.JLabel();
        endField = new javax.swing.JTextField();
        plateLabel = new javax.swing.JLabel();
        plateField = new javax.swing.JTextField();
        dateLabel = new javax.swing.JLabel();
        dateStartField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        dateEndField = new javax.swing.JTextField();
        boundaryLabel = new javax.swing.JLabel();
        boundaryField = new javax.swing.JTextField();
        tablePane = new javax.swing.JScrollPane();
        operationsTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        saveButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        commentLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        searchButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        dataPanel.setLayout(new java.awt.GridLayout(9, 2, 4, 2));

        numberLabel.setText("Taxi Number");
        dataPanel.add(numberLabel);

        taxiNumberCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                taxiNumberComboActionPerformed(evt);
            }
        });
        dataPanel.add(taxiNumberCombo);

        driverLabel.setText("Driver/Operator");
        dataPanel.add(driverLabel);

        dataPanel.add(driverCombo);

        jLabel2.setText("Name of Driver/Oprtr");
        dataPanel.add(jLabel2);
        dataPanel.add(jTextField1);

        startLabel.setText("Start Odometer");
        dataPanel.add(startLabel);
        dataPanel.add(startField);

        endLabel.setText("End Odometer");
        dataPanel.add(endLabel);
        dataPanel.add(endField);

        plateLabel.setText("Plate Number");
        dataPanel.add(plateLabel);
        dataPanel.add(plateField);

        dateLabel.setText("Start Date");
        dataPanel.add(dateLabel);
        dataPanel.add(dateStartField);

        jLabel1.setText("End Date");
        dataPanel.add(jLabel1);
        dataPanel.add(dateEndField);

        boundaryLabel.setText("Boundary");
        dataPanel.add(boundaryLabel);
        dataPanel.add(boundaryField);

        getContentPane().add(dataPanel);
        dataPanel.setBounds(0, 0, 280, 260);

        ResultSet rs = getResultFromDB();
        operationsTable.setAutoCreateRowSorter(true);
        operationsTable.setModel(new OperationsTableModel(rs)
        );
        operationsTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tablePane.setViewportView(operationsTable);

        getContentPane().add(tablePane);
        tablePane.setBounds(290, 0, 720, 450);

        jPanel2.setLayout(new java.awt.GridLayout(4, 0));

        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        jPanel2.add(saveButton);

        editButton.setText("Edit");
        editButton.setEnabled(false);
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });
        jPanel2.add(editButton);

        deleteButton.setText("Delete");
        deleteButton.setEnabled(false);
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        jPanel2.add(deleteButton);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        jPanel2.add(cancelButton);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(0, 290, 280, 90);

        jLabel8.setText("Date Format YYY-DD-MM");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(40, 270, 160, 14);
        getContentPane().add(commentLabel);
        commentLabel.setBounds(-90, 300, 140, 20);

        jLabel3.setText("From Date");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 390, 50, 14);

        jLabel4.setText("To Date");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(10, 420, 50, 14);

        jTextField2.setText("jTextField2");
        getContentPane().add(jTextField2);
        jTextField2.setBounds(90, 390, 59, 20);

        jTextField3.setText("jTextField3");
        getContentPane().add(jTextField3);
        jTextField3.setBounds(90, 420, 59, 20);

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });
        getContentPane().add(searchButton);
        searchButton.setBounds(180, 400, 73, 23);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-1029)/2, (screenSize.height-495)/2, 1029, 495);
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
//        String unitnumber=String.valueOf(taxiNumberCombo.getSelectedItem());
//        String driver=(String)driverCombo.getSelectedItem();
//        String startodometer=startField.getText();
//        String endodometer=endField.getText();
//        String platenumber=plateField.getText();
//        String date=dateStartField.getText();
//        String boundary=boundaryField.getText();
//        try{
//            int done = operationsDAO.saveOperation(new Operation(unitnumber,
//                    driver, startodometer, endodometer, platenumber, date, boundary));
//
//            commentLabel.setText("1 row inserted");
//            updateOperationTable();
//        }
//        catch(Exception e){
//            commentLabel.setText("Error occurred in inserting data");
//            e.printStackTrace();
//        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
         this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_searchButtonActionPerformed

    private void taxiNumberComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_taxiNumberComboActionPerformed
        
    }//GEN-LAST:event_taxiNumberComboActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed

//        Operation operation = getOperationFromTable();
//        JFrame editFrame = new JFrame("Edit Operation");
//        editFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
//        editFrame.setLocationRelativeTo(this);
//        OperationsEditUI editUI = new OperationsEditUI(operation, editFrame, this);
//        editFrame.getContentPane().add(editUI);
//        editFrame.pack();
//        editFrame.setVisible(true);
    }//GEN-LAST:event_editButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        try {
            operationsDAO.deleteOperation(getOperationFromTable());
            updateOperationTable();
        } catch (SQLException ex) {
           ex.printStackTrace();
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField boundaryField;
    private javax.swing.JLabel boundaryLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel commentLabel;
    private javax.swing.JPanel dataPanel;
    private javax.swing.JTextField dateEndField;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JTextField dateStartField;
    private javax.swing.JButton deleteButton;
    private javax.swing.JComboBox driverCombo;
    private javax.swing.JLabel driverLabel;
    private javax.swing.JButton editButton;
    private javax.swing.JTextField endField;
    private javax.swing.JLabel endLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JLabel numberLabel;
    private javax.swing.JTable operationsTable;
    private javax.swing.JTextField plateField;
    private javax.swing.JLabel plateLabel;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField startField;
    private javax.swing.JLabel startLabel;
    private javax.swing.JScrollPane tablePane;
    private javax.swing.JComboBox taxiNumberCombo;
    // End of variables declaration//GEN-END:variables

}
